/** <!--------------------------------------------------------------------------------------------->
 * @file main.cpp
 *
 * GSvn - A Graphical Client for SVN
 * Copyright (C) 2012  Karl Krach, gsvn@bluespirit.la
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 <!---------------------------------------------------------------------------------------------> */

// -- includes --
#include "RevGraphApplication.h"
#include "SvnConnection.h"
#include "RevModel.h"
#include "RevGraph.h"
#include <QDebug>
#include <QThread>


/** <!--------------------------------------------------------------------------------------------->
 * Application's entry point
 <!---------------------------------------------------------------------------------------------> */
int main( int argc, char *argv[] )
{
	// Set current directory as default path
	const char* strPath = ".";

	// Use first argument as path - if present
	if( argc > 0 )
	{
		strPath = argv[1];
		qDebug() << "Requsting SVN Log for" << strPath;
	}


	QTime oStopWatch;
	oStopWatch.start();

	//
	// Initialize Qt
	//
	RevGraphApplication a(argc, argv);
	qDebug() << oStopWatch.elapsed() << " Qt initialized";

	//
	// Create model
	//
	RevModel oModel;
	QThread oDataThread;
	oModel.moveToThread( &oDataThread );
	qDebug() << oStopWatch.elapsed() << " Model created";
	oDataThread.start();

	oModel.setBranchPattern( QRegExp("^(\\/wiki)|(\\/trunk)|(\\/branches\\/[^/]+)") );

	//
	// Create widget
	//
	RevGraph oGraph;
	oGraph.setMinimumSize( 800, 600 );
	oGraph.show();
	qDebug() << oStopWatch.elapsed() << " Graph opened";

	QMetaTypeId<RevModelItem>::qt_metatype_id();
	QMetaTypeId< QSP<RevModelItem> >::qt_metatype_id();
	QMetaTypeId<SvnRevisionIdentifier>::qt_metatype_id();
	QMetaTypeId<BranchIndex>::qt_metatype_id();

	if( !QObject::connect( &oGraph, SIGNAL(SI_RevisionRequested( SvnRevisionIdentifier )),
	                       &oModel, SLOT(SL_TriggerRevisionBroadcast( SvnRevisionIdentifier ))
	                       )
	   )
	{
		qWarning() << "Cannot connect SI_RevisionRequested!";
		return 250;
	}
	if( !QObject::connect( &oModel, SIGNAL(SI_BranchChanged( BranchIndex, QString )),
	                       &oGraph, SLOT(SL_UpdateBranch( BranchIndex, QString )) ) )
	{
		qWarning() << "Cannot connect SI_BranchChanged!";
		return 251;
	}
	if( !QObject::connect( &oModel, SIGNAL(SI_RevisionChanged( QSP<RevModelItem> )),
	                       &oGraph, SLOT(SL_UpdateRevision( QSP<RevModelItem> )) ) )
	{
		qWarning() << "Cannot connect SI_RevisionChanged!";
		return 252;
	}
	if( !QObject::connect( &oModel, SIGNAL(SI_StatusChanged( QString )),
	                       &oGraph, SLOT(SL_VisualizeStatus( QString )) ) )
	{
		qWarning() << "Cannot connect SI_StatusChanged!";
		return 253;
	}


//	const SvnRevisionIdentifier oNodeID = { "https://svndemoproject.googlecode.com/svn/trunk/doc/index.html",
//	                                        true, 4 };
	const SvnRevisionIdentifier oNodeID = { "https://gsvn.googlecode.com/svn",
	                                        true, 4 };
//	const SvnRevisionIdentifier oNodeID = { strPath, true, 999 };
	oGraph.Visualize( oNodeID );

	return a.exec();


}	// END main( int, char** )
